package com.xiaoka.camelv.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.xiaoka.camelv.constant.Constant;
import com.xiaoka.camelv.constant.ResourceType;
import com.xiaoka.camelv.constant.RouteType;
import com.xiaoka.camelv.dao.CamelvLineDao;
import com.xiaoka.camelv.dao.CamelvRouteDao;
import com.xiaoka.camelv.entity.CamelvLine;
import com.xiaoka.camelv.entity.CamelvRoute;
import com.xiaoka.camelv.entity.vo.ResponseData;
import com.xiaoka.camelv.service.ICamelvRouteService;
import com.xiaoka.camelv.service.ICamelvServerService;

@Service
public class CamelvRouteServiceImpl extends BaseService implements ICamelvRouteService {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private CamelvRouteDao routeDao;
	@Autowired
	private CamelvLineDao lineDao;
	@Autowired
	private CamelvResourceHttpPushServiceImpl pushDataService;
	@Autowired
	private ICamelvServerService serverService;

	@Override
	public ResponseData save(CamelvRoute route) {
		ResponseData rd = new ResponseData(Constant.ERROR);
		try {
			CamelvRoute dbRoute = get(route.getId());
			if (dbRoute == null) {
				// 新加
				route.setId(null);
				route.setWidth(100);
				route.setHeight(35);
				routeDao.save(route);
				rd.setType(Constant.SUCCESS);
				rd.setData(route.getId());
				pushDataService.pushCamelvRoute(route);
				return rd;
			}
		} catch (Exception e) {
			logger.error("保存路由失败");
			e.printStackTrace();
		}
		return rd;
	}

	@Override
	public List<CamelvRoute> getByServerId(String serverId) {
		try {
			DetachedCriteria dc = routeDao.createDetachedCriteria();
			dc.add(Restrictions.eq(CamelvRoute.FIELD_DEL_FLAG, CamelvRoute.DEL_FLAG_NORMAL));
			dc.add(Restrictions.eq("serverId", serverId));
			List<CamelvRoute> list = routeDao.find(dc);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<CamelvRoute>();
	}

	@Override
	public CamelvRoute get(String id) {
		try {
			return routeDao.get(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public ResponseData update(CamelvRoute route) {
		ResponseData rd = new ResponseData(Constant.ERROR);
		try {
			String option = "";
			if (!StringUtils.isBlank(route.getOption())) {
				// 转义
				option = StringEscapeUtils.unescapeHtml(route.getOption());
			}
			// 这里需要根据不同类型做判断
			route.setOption(option);
			routeDao.save(route);
			rd.setType(Constant.SUCCESS);
			pushDataService.pushCamelvRoute(route);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rd;
	}

	@Override
	public ResponseData move(CamelvRoute route) {
		ResponseData rd = new ResponseData(Constant.ERROR);
		try {
			CamelvRoute dbRoute = routeDao.get(route.getId());
			// 更新坐标
			dbRoute.setLeft(route.getLeft());
			dbRoute.setTop(route.getTop());
			routeDao.save(dbRoute);
			rd.setType(Constant.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rd;
	}

	@Override
	public ResponseData delete(String id) {
		ResponseData rd = new ResponseData(Constant.ERROR);
		try {
			// 删除与该节点的所有关系线
			DetachedCriteria lineDc = lineDao.createDetachedCriteria();
			lineDc.add(Restrictions.eq(CamelvLine.FIELD_DEL_FLAG, CamelvLine.DEL_FLAG_NORMAL));
			lineDc.add(Restrictions.or(Restrictions.eq("from", id), Restrictions.eq("to", id)));
			List<CamelvLine> lineList = lineDao.find(lineDc);
			for (CamelvLine line : lineList) {
				lineDao.deleteById(line.getId());
			}
			// 删除路由本身
			routeDao.deleteById(id);
			rd.setType(Constant.SUCCESS);
			pushDataService.delete(ResourceType.RES_TYPE_ROUTE, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rd;
	}

	@Override
	public ResponseData rename(String id, String newName) {
		ResponseData rd = new ResponseData(Constant.ERROR);
		try {
			CamelvRoute dbRoute = routeDao.get(id);
			dbRoute.setName(newName);
			routeDao.save(dbRoute);
			rd.setType(Constant.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rd;
	}

	@Override
	public ResponseData resize(String id, Integer width, Integer height) {
		ResponseData rd = new ResponseData(Constant.ERROR);
		try {
			CamelvRoute dbRoute = routeDao.get(id);
			dbRoute.setWidth(width);
			dbRoute.setHeight(height);
			routeDao.save(dbRoute);
			rd.setType(Constant.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rd;
	}

	public static void main(String[] args) {
		String option = "option=name-&gt;tom,ddd-&gt;111,";
		// option =
		// StringEscapeUtils.escapeHtml(StringEscapeUtils.unescapeHtml(option));
		option = StringEscapeUtils.unescapeHtml(option);
		System.out.println(option);

	}

	@Override
	@Transactional(readOnly = false)
	public List<CamelvRoute> getAll() {
		try {
			DetachedCriteria dc = routeDao.createDetachedCriteria();
			dc.add(Restrictions.eq(CamelvRoute.FIELD_DEL_FLAG, CamelvRoute.DEL_FLAG_NORMAL));
			return routeDao.find(dc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<CamelvRoute>();
	}

	@Override
	public String getServerUrl(String serverId) {
		try {
			DetachedCriteria dc = routeDao.createDetachedCriteria();
			dc.add(Restrictions.eq(CamelvRoute.FIELD_DEL_FLAG, CamelvRoute.DEL_FLAG_NORMAL));
			dc.add(Restrictions.eq("type", RouteType.ROUTE_TYPE_JETTY));
			dc.add(Restrictions.eq("serverId", serverId));
			List<CamelvRoute> list = routeDao.find(dc);
			return list == null || list.size() == 0 ? "" : list.get(0).getUri();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

}